import {reqRegister} from "../../request/api"
//初始值
const initState = {
    user:{
        phone:"",
        nickname:"",
        password:""
    }
}

//actionTypes
const types = {
    //修改 
    CHANGE_USER:"REGISTER_CHANGE_USER",
}

//reducer 修改数据
const reducer = (state = initState, action) => {
    switch (action.type) {
        //修改 user
        case types.CHANGE_USER:
            return {
                ...state,
                user:{
                    ...state.user,
                    [action.key]:action.value
                }
            }
            
        default:
            return state;
    }

}

//actionCreators组件派发任务
export const actions = {
    //修改，请求
    changeUser:(key,value)=>({type:types.CHANGE_USER,key,value}),
    //history,调用时解析传参
    reqRegisters:(history)=>(dispatch,getState)=>{
        //解析已经改变的state  user
        // console.log(getState());此时的state为index-reducer
        let {user}=getState().register;
        reqRegister(user).then(res=>{
            if(res.data.code===200){
                history.push("/login")
            }
        })
    }

}

//selectors 导出数据   此时的state为index-reducer
export let getUser =state=>state.register.user

export default reducer